﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;
using FilStartUps.Helper;

namespace FilStartUps.Controls
{
    public partial class FilStartUps_ForumDisplay : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {
                    string forumidPK = Request.QueryString["f"].Trim();
                    if (forumidPK != "")
                    {
                        GetForumDisplay(forumidPK);
                    }
                }
                catch (Exception)
                {

                }
            }

        }

        private void GetForumDisplay(string forumid)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {



                //var forum = db_connect.ForumThreads
                //                .Where(f => f.forumidFK.Equals(forumid))
                //                .ToList();
                int forumidPK = int.Parse(forumid);
                var forum = (from ft in db_connect.ForumThreads
                              where ft.forumidFK == forumidPK 
                            let cCount =
                               (from fr in db_connect.ForumReplies
                                where ft.forumthreadidPK == fr.forumthreadidFK
                                select fr).Count()
                            select new 
                            {
                                forumthreadidPK =ft.forumthreadidPK,
                                forumdescription = ft.forumdescription,
                                totalpost = cCount,
                                forumtitle = ft.forumtitle,
                                forumidFK = ft.forumidFK,
                                totalviews = ft.views
                            }).ToList();


                //var forum_ = (from f in db_connect.ForumThreads
                //              join fr in db_connect.ForumReplies
                //              on f.forumthreadidPK equals fr.forumthreadidFK
                //              where f.forumidFK.Equals(forumid)
                //              select new ForumThreadTable
                //              {
                //                  forumdescription = f.forumdescription,
                //                  forumidFK = f.forumidFK,
                //                  forumtitle = f.forumtitle,
                //                  totalpost = forum.Count()

                //              }).ToList();

                if (forum.Count() > 0)
                {

                    dgforumthread.DataSource = forum;
                    dgforumthread.DataBind();

                    if (forum.Count() < dgforumthread.PageSize)
                    {
                        dgforumthread.PagerStyle.Visible = false;
                    }

                }
                else
                {
                    dgforumthread.Visible = false;  
                    lterror.Visible = true;
                }
            }
        }

        public string truncate(string desc)
        {
            string desc_;
            try
            {
                desc_ = desc.ToString().Substring(0, 100) + "...";
            }
            catch
            {
                desc_ = desc;
            }
            return desc_;
        }

        private void ForumThreadAddViews(int forumthreadid)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var forumthread = db_connect.ForumThreads.Find(forumthreadid);
                forumthread.views += 1;
                db_connect.SaveChanges();
            }
        }

        protected void dgforumthread_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                LinkButton linkforumtitle = (LinkButton)dgforumthread.Items[e.Item.ItemIndex].FindControl("hlForum");
                Label lbforumthreadid = (Label)dgforumthread.Items[e.Item.ItemIndex].FindControl("lbforumid");

                ForumThreadAddViews(Convert.ToInt32(lbforumthreadid.Text));

                Response.Redirect("~/ShowThread.aspx?t=" + lbforumthreadid.Text);
            }
        }

        protected void dgforumthread_Paged(object sender, DataGridPageChangedEventArgs e)
        {
            dgforumthread.CurrentPageIndex = e.NewPageIndex;
            GetForumDisplay(Request.QueryString["f"].Trim());
        }

        protected void lbcreatethread_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/NewThread.aspx?f=" + Convert.ToInt32(Request.QueryString["f"].Trim()));
        }
    }
}